package org.grottarossa.ui.core.casemanagement.tasks;

import java.util.ResourceBundle;

import org.grottarossa.driver.ECMDriver;
import org.grottarossa.template.drivers.DriverException;
import org.grottarossa.ui.core.ApplicationSession;
import org.grottarossa.ui.core.FieldComponent;
import org.grottarossa.ui.core.i18n.DM;


import com.vaadin.data.Item;
import com.vaadin.ui.Component;
import com.vaadin.ui.DefaultFieldFactory;
import com.vaadin.ui.Field;
import com.vaadin.ui.TextField;

public class TaskFormFactory extends DefaultFieldFactory
{
	private static final long serialVersionUID = 1L;

	private static final String COMMON_FIELD_WIDTH = "12em";

	ResourceBundle localizer = DM.instance().getBundle(getClass().getName());

	private final ApplicationSession m_appSession;

	public TaskFormFactory(ApplicationSession appSession)
	{
		this.m_appSession = appSession;
	}

	@Override
	public Field createField(Item item, Object propertyId, Component uiContext) {

		if("compito".equals(propertyId))
		{
			TextField tf = new TextField(localizer.getString("task.form.compito"));
			tf.setReadOnly(true);
            tf.setWidth(COMMON_FIELD_WIDTH);
            return tf;
		}
		else if("stato".equals(propertyId))
		{
			try
			{
				return FieldComponent.getInitComboBox(m_appSession,
												localizer.getString("task.form.stato"),
												ECMDriver.SpecialWords.BPM_CASE_TASK_STATUS,
												localizer.getString("task.form.stato.error"));
			}
			catch (DriverException e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		else if("completato".equals(propertyId))
		{
			TextField tf = new TextField(localizer.getString("task.form.completato"));
            tf.setWidth(COMMON_FIELD_WIDTH);
            return tf;
		}
		else if("assegnatoA".equals(propertyId))
		{
			try
			{
				return FieldComponent.getAssigneeUsers(m_appSession, "Assegnatario", m_appSession.getBuilder().getUserManagement().getUser(m_appSession.getBuilder().getIdentity().getUser()).getRoles());
			}
			catch (DriverException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return super.createField(item, propertyId, uiContext);
	}
}
